﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeC.Interfaces;
using System.ComponentModel.Composition;


namespace CodeC.Utilities
{

    /// <summary>
    /// This class generate the code console application code based on the CodeCModel
    /// </summary>
    [Export(typeof(ICreateClassCode))]
    public class CodeGenerator : ICreateClassCode
    {
        #region Constant
        string CODEMAIN = "[NAMESPACES]" +
                    "namespace CodeCGenerator" +
                    "{" +
                    "[T]class Program" +
                    "[T]{" +
                    "[T][T]static void Main(string[] args)" +
                    "[T][T]{" +
                    "[T][T][T](new CodeCClass()).Run();" +
                    "[T][T]}" +
                    "[T]}" +
                    "[T]public class CodeCClass" +
                    "[T]{" +
                    "[T][T]public void Run()" +
                    "[T][T]{" +
                    "[NEWLINE][MAINCODECONTENT][NEWLINE]" +
                    "}" +
                    "" +
                    "[NEWLINE][CLASSMETHODS][NEWLINE]" +
                    "[T]}" +
                    "}";



        #endregion

        #region Interface Implementation

        public string GenerateClassString(ICodeCModel _model)
        {
            StringBuilder sbNamespaces = new StringBuilder();

            if (null != _model.ListOfNamespaces)
                foreach (var item in _model.ListOfNamespaces)
                    sbNamespaces.Append(string.Format("using {0};{1}", item, Environment.NewLine));

            return CODEMAIN
                .ReplaceMainCodeContent(_model.MainCode ?? "")
                .ReplaceClassMethods(_model.ClassMethodsCode ?? "")
                .ReplaceNamespaces(sbNamespaces.ToString())
                .ReplaceConsoleRead()
                .ReplaceNewLine();
        }

        #endregion
    }
}
